package cn.hnbjx.prepaid.common;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class AdminInterceptor implements HandlerInterceptor {
    /**
     * 在执行controller方法之前执行,如果返回true代表放行,如果返回false代表不放行。
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        Integer adminId = (Integer) request.getSession().getAttribute("adminid");
        if (null == adminId) {    //代表用户没有登陆
            throw new BusinessException(BusinessStatus.NOT_LOGIN);
        }
        return isLogin(request);    // 如果没进入if表示登录了,可以访问controller
    }
    /**
     * 判断当前用户是否登录
     *
     * @param request
     * @return
     */
    private boolean isLogin(HttpServletRequest request) {
        HttpSession session = request.getSession();
        if (session.getAttribute("isLogin") == null) {
            throw new BusinessException(BusinessStatus.NOT_LOGIN);
        }
        return true;
    }
}